package size;

import java.util.Stack;

public class Jisuan {

    String [] str1;
    String result;//结果储存

    public Jisuan (String jisuan)//构造用于计算一个后缀表达式的函数
    {
        str1=jisuan.split("\\s");
    }

    public RationalNumber toRationalNumber(String num) //转换为RationalNumber类的对象的方法
    {

        if (num.contains("/")==false){
            int a =Integer.parseInt(num);
            RationalNumber rationalNumber1 =new RationalNumber(a,1);
            return rationalNumber1;
        }
        else {

            String[] Array =num.split("/");
            int numerator = Integer.parseInt(Array[0]);
            int denominator=Integer.parseInt(Array[1]);

            RationalNumber rationalNumber2 =new RationalNumber(numerator,denominator);
            return rationalNumber2;
        }
    }


    public void ToResult()//计算中缀表达式，转移结果到先前声明的result
    {
        Stack stack1=new Stack();
        int start =0;

        while (start<str1.length){
            if (str1[start].equalsIgnoreCase("+")==false&&str1[start].equalsIgnoreCase("-")==false&&str1[start].equalsIgnoreCase("×")==false&&str1[start].equalsIgnoreCase("÷")==false){

                stack1.push(str1[start]);
            }
            else
            if (str1[start].equalsIgnoreCase("+")==true){
                RationalNumber num1=this.toRationalNumber(String.valueOf(stack1.peek()));
                stack1.pop();
                RationalNumber num2=this.toRationalNumber(String.valueOf(stack1.peek()));
                stack1.pop();
                RationalNumber finish =num2.add(num1);
                String str3=finish.toString();
                stack1.push(str3);
            }
            else
            if (str1[start].equalsIgnoreCase("-")==true){
                RationalNumber num1=this.toRationalNumber(stack1.peek().toString());
                stack1.pop();
                RationalNumber num2=this.toRationalNumber(stack1.peek().toString());
                stack1.pop();
                RationalNumber finish =num2.subtract(num1);
                String str3=finish.toString();
                stack1.push(str3);
            }
            else
            if (str1[start].equalsIgnoreCase("×")==true){
                RationalNumber num1=this.toRationalNumber(stack1.peek().toString());
                stack1.pop();
                RationalNumber num2=this.toRationalNumber(stack1.peek().toString());
                stack1.pop();
                RationalNumber finish =num2.multiply(num1);
                String str3=finish.toString();
                stack1.push(str3);
            }
            else
            {
                RationalNumber num1=this.toRationalNumber(String.valueOf(stack1.peek()));
                stack1.pop();
                RationalNumber num2=this.toRationalNumber(String.valueOf(stack1.peek()));
                stack1.pop();
                RationalNumber finish =num2.divide(num1);
                String str3=finish.toString();
                stack1.push(str3);
            }

            start++;
        }
        String str4=stack1.peek().toString();
        result=str4;
    }

    public String getResult()//计算结果
    {
        return result;
    }

}
